﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Net;
using System.Drawing.Imaging;

namespace Jy123.Web.AppCode
{
    /********************************************************************************
        ** 作者： Pengyulong
        ** 创始时间： 2009-5-7
        ** 描述：用于创建图片上传所需要保存的文件夹，返回文件按照命名规范后生成的文件名
        ** 备注：所有的方法的参数type就是下面定义的const string，调用方法时使用CreateDir.LOGO传入即可
        *********************************************************************************/
    public class CreateDir : System.Web.UI.Page
    {
        private string mapPath = string.Empty;
        private const string ROOT = "/JyIMG";
        /// <summary>
        /// 表示图片是Logo
        /// </summary>
        public const string LOGO = "/Logo";
        /// <summary>
        /// 表示图片是属于组件等级的
        /// </summary>
        public const string LEVEL = "/Level";
        /// <summary>
        /// 表示图片是属于赛场的
        /// </summary>
        public const string VENUES = "/Venues";
        /// <summary>
        /// 表示图片是属于俱乐部的
        /// </summary>
        public const string CLUB = "/Club";
        /// <summary>
        /// 表示图片是属于训练中心的
        /// </summary>
        public const string TCENTER = "/TCenter";
        /// <summary>
        /// 表示图片是属于用户的
        /// </summary>
        public const string USER = "/User";
        /// <summary>
        /// 表示图片是属于群的
        /// </summary>
        public const string GROUP = "/Group";
        private const string PIC = "/Pic";
        /// <summary>
        /// 表示图片是队服图片
        /// </summary>
        public const string UNIFORMS = "/Uniforms";
        /// <summary>
        /// 表示图片是道具图片
        /// </summary>
        public const string PROPS = "/Props";
        /// <summary>
        /// 表示图片是明星或者教练图片
        /// </summary>
        public const string STAR = "/Star";
        /// <summary>
        /// 表示图片是运动类型图标
        /// </summary>
        public const string SPORTTYPE = "/SportType";
        /// <summary>
        /// 表示图片是状态图标
        /// </summary>
        public const string ACTION = "/Action";
        /// <summary>
        /// 表示图片是表情图标
        /// </summary>
        public const string EXPRESSION = "/Expression";
        /// <summary>
        /// 表示图片是礼物图片
        /// </summary>
        public const string GIFT = "/Gift";
        /// <summary>
        /// 表示图片是赛车小图片
        /// </summary>
        public const string SMALLCARS = "/SmallCars";
        /// <summary>
        /// 表示图片是赛车大图片
        /// </summary>
        public const string BIGCARS = "/BigCars";
        /// <summary>
        /// 表示图片是赛车配件图片
        /// </summary>
        public const string FITTINGS = "/Fittings";
        /// <summary>
        /// 表示图片是设备图片
        /// </summary>
        public const string ANNEXE = "/Annexe";
        /// <summary>
        /// 表示图片是维护定义图片
        /// </summary>
        public const string MAINTAIN = "/Maintain";
        /// <summary>
        /// 表示图片是租用定义图片
        /// </summary>
        public const string HIRE = "/Hire";
        /// <summary>
        /// 表示图片是阵型图片
        /// </summary>
        public const string FORM = "/Form";
        public CreateDir()
        {
            mapPath = Server.MapPath("/");
        }
        /// <summary>
        /// 创建组件LOGO图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件类型</param>
        /// <param name="moduleId">组件ID</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateModuleLogPath(string type, int moduleId, string picType, string modelType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + modelType;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + LOGO + "_" + moduleId.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建用户或者群LOGO图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">是用户还是群</param>
        /// <param name="id">用户或者群ID</param>
        /// <param name="createTime">用户或者群创建时间</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateUserLogPath(string type, int id, DateTime createTime, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + createTime.Year.ToString() + "/" +
                createTime.Month.ToString() + "/" + createTime.Day.ToString() + "/" + id.ToString();
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + LOGO + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建组件大图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件类型</param>
        /// <param name="level">组件等级</param>
        /// <param name="strColor">所选颜色，使用FFBBCC格式</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateModulePicPath(string type, int level, string strColor, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + level.ToString();
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + PIC + "_" + strColor + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建队服图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件类型</param>        
        /// <param name="id">图片id</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateUniformPath(string type, int id, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建租用定义图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件类型</param>        
        /// <param name="id">图片id</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateHirePath(string type, int id, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建组件维护图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件类型</param>        
        /// <param name="id">图片id</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreateMaintainPath(string type, int id, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建道具图片存放文件夹，并返回道具图片文件名
        /// </summary>
        /// <param name="type">类型</param>        
        /// <param name="level">道具等级</param>
        /// <param name="propsId">道具ID</param>
        /// <param name="picType">图片后缀名</param>
        /// <returns>文件完整路径名</returns>
        public string CreatePropsPath(string type, int propsId, string picType, string modelType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + modelType;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + propsId.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建明星或教练图片存放文件夹，并返回明星或教练图片文件名
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="sportType">运动类型</param>
        /// <param name="typeId">明星或教练或其他</param>
        /// <param name="starId">明星或教练类型</param>
        /// <param name="picType">图片后缀名</param>
        /// <param name="modelType">图标类型</param>
        /// <returns>文件完整路径名</returns>
        public string CreateStarPath(string type, int sportType, int typeId, int starId, string picType, string modelType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + modelType;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + sportType.ToString() + "_" + typeId.ToString() + "_" +
                starId.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建小图标存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">小图标类型</param>
        /// <param name="id">小图标id</param>
        /// <param name="picType">小图标文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreateIconPath(string type, int id, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建赛车配件存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="id">配件id</param>
        /// <param name="picType">图片文件格式</param>
        /// <param name="modelType">图片类型，是大图片还是小图片</param>
        /// <returns>文件完整路径名</returns>
        public string CreateFittingsPath(string type, int id, string picType,string modelType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + modelType;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建设备存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="id">设备id</param>
        /// <param name="picType">图片文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreateAnnexePath(string type, int id, string picType, string modelType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + modelType;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建礼物图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="id">礼物图片id</param>
        /// <param name="picType">礼物图片文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreateGiftPath(string type, int id, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type;
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + "/" + id.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建用户图片和群图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">用户图片或群图片</param>
        /// <param name="id">用户ID或群ID</param>
        /// <param name="createTime">用户或群创建时间</param>
        /// <param name="picType">图片文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreatePhotoPath(string type, int id, DateTime createTime, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + createTime.Year.ToString() + "/" +
                createTime.Month.ToString() + "/" + createTime.Day.ToString() + "/" + id.ToString();
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + type + "_" + createTime.Ticks + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建组件等级图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">组件等级图片</param>
        /// <param name="id">用户ID或群ID</param>
        /// <param name="createTime">用户或群创建时间</param>
        /// <param name="picType">图片文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreateLevelPath(string type, int id, DateTime createTime, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + createTime.Year.ToString() + "/" +
                createTime.Month.ToString() + "/" + createTime.Day.ToString() + "/" + id.ToString();
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + type + "_" + createTime.Ticks + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 创建用户相册封面图片和群相册封面图片存放文件夹，并返回图片文件名
        /// </summary>
        /// <param name="type">用户或者群</param>
        /// <param name="id">用户ID或者群ID</param>
        /// <param name="albumId">相册ID</param>
        /// <param name="createTime">用户或者群创建时间</param>
        /// <param name="picType">图片文件格式</param>
        /// <returns>文件完整路径名</returns>
        public string CreateAlbumPath(string type, int id, int albumId, DateTime createTime, string picType)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            strDirectory = ROOT + type + "/" + createTime.Year.ToString() + "/" +
                createTime.Month.ToString() + "/" + createTime.Day.ToString() + "/" + id.ToString();
            if (!Directory.Exists(mapPath + strDirectory))
            {
                Directory.CreateDirectory(mapPath + strDirectory);
            }
            strPicName = strDirectory + albumId.ToString() + "." + picType;
            return strPicName;
        }

        /// <summary>
        /// 根据传入的文件名和大小查找缩略图，如果缩略图不存在就创建缩略图
        /// </summary>
        /// <param name="width">裁剪后图宽度，如果输入为零，那么就根据比例计算缩略图的宽度</param>
        /// <param name="height">裁剪后图高度，如果输入为零，那么就根据比例计算缩略图的高度</param>
        /// <param name="x1">裁剪起始X轴</param>
        /// <param name="x2">裁剪结束X轴</param>
        /// <param name="y1">裁剪起始Y轴</param>
        /// <param name="y2">裁剪结束Y轴</param>
        /// 如果高度宽度同时输入0，那么检查你自己的程序
        /// <param name="filePath">原图位置</param>
        /// <returns>裁剪后图完整路径名</returns>
        public string CutImage(int width, int height, int x1, int x2, int y1, int y2, string filePath)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            int lastIndex = filePath.LastIndexOf("/");
            if (!string.IsNullOrEmpty(filePath))
            {
                string strPath = filePath.Substring(0, lastIndex);
                string strMiddlePath = "/" + width.ToString() + "_" + height.ToString();
                string fileName = filePath.Substring(lastIndex + 1);
                strDirectory = strPath + strMiddlePath;
                strPicName = strDirectory + "/" + fileName;
                if (File.Exists(mapPath + filePath))
                {
                    if (!Directory.Exists(mapPath + strDirectory))
                    {
                        Directory.CreateDirectory(mapPath + strDirectory);
                    }

                    Bitmap originaImage = new Bitmap(mapPath + filePath);
                    int oWidth = x2 - x1;
                    int oHeight = y2 - y1;
                    if (width == 0)
                    {
                        width = height * oWidth / oHeight;
                    }
                    else if (height == 0)
                    {
                        height = width * oHeight / oWidth;
                    }

                    Bitmap image = new Bitmap(width, height);
                    Rectangle originalRec = new Rectangle(x1, y1, oWidth, oHeight);
                    Rectangle rec = new Rectangle(0, 0, width, height);
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(originaImage, rec, originalRec, GraphicsUnit.Pixel);
                    ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo ici = null;
                    foreach (ImageCodecInfo i in icis)
                    {
                        if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
                        {
                            ici = i;
                        }
                    }
                    EncoderParameters ep = new EncoderParameters(1);
                    ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
                    try
                    {
                        image.Save(mapPath + strPicName, ici, ep);
                    }
                    catch
                    {
                    }
                    originaImage.Dispose();
                    image.Dispose();
                    g.Dispose();
                    ep.Dispose();
                }
            }
            return strPicName;
        }

        /// <summary>
        /// 根据传入的文件名和大小查找缩略图，如果缩略图不存在就创建缩略图
        /// </summary>
        /// <param name="width">缩略图宽度，如果输入为零，那么就根据比例计算缩略图的宽度</param>
        /// <param name="height">缩略图高度，如果输入为零，那么就根据比例计算缩略图的高度</param>
        /// 如果高度宽度同时输入0，那么检查你自己的程序
        /// <param name="filePath">原图位置</param>
        /// <returns>缩略图完整路径名</returns>
        public string ReDraw(int width, int height, string filePath)
        {
            string strPicName = string.Empty;
            string strDirectory = string.Empty;
            int lastIndex = filePath.LastIndexOf("/");
            if (!string.IsNullOrEmpty(filePath))
            {
                string strPath = filePath.Substring(0, lastIndex);
                string strMiddlePath = "/" + width.ToString() + "_" + height.ToString();
                string fileName = filePath.Substring(lastIndex + 1);
                strDirectory = strPath + strMiddlePath;
                strPicName = strDirectory + "/" + fileName;
                if (File.Exists(mapPath + filePath))
                {
                    if (!File.Exists(strPicName))
                    {
                        if (!Directory.Exists(mapPath + strDirectory))
                        {
                            Directory.CreateDirectory(mapPath + strDirectory);
                        }

                        Bitmap originaImage = new Bitmap(mapPath + filePath);
                        int oWidth = originaImage.Width;
                        int oHeight = originaImage.Height;
                        if (width == 0)
                        {
                            width = height * oWidth / oHeight;
                        }
                        else if (height == 0)
                        {
                            height = width * oHeight / oWidth;
                        }

                        Bitmap image = new Bitmap(width, height);
                        Rectangle originalRec = new Rectangle(0, 0, originaImage.Width, originaImage.Height);
                        Rectangle rec = new Rectangle(0, 0, width, height);
                        Graphics g = Graphics.FromImage(image);
                        g.DrawImage(originaImage, rec, originalRec, GraphicsUnit.Pixel);
                        ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo ici = null;
                        foreach (ImageCodecInfo i in icis)
                        {
                            if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
                            {
                                ici = i;
                            }
                        }
                        EncoderParameters ep = new EncoderParameters(1);
                        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)75);
                        try
                        {
                            image.Save(mapPath + strPicName, ici, ep);
                        }
                        catch
                        {
                        }
                        originaImage.Dispose();
                        image.Dispose();
                    }
                }
            }
            return strPicName;
        }
        /// <summary>
        /// 下载外网图片
        /// </summary>
        /// <param name="url">图片URL</param>
        /// <param name="filePath">存放路径</param>
        /// <returns>成功失败</returns>
        private bool DownloadImg(string url, string filePath)
        {
            WebClient webclient = new WebClient();
            string imgFileName = url.Substring(url.LastIndexOf('/') + 1, url.Length - url.LastIndexOf('/') - 1);
            try
            {
                if (!Directory.Exists(mapPath + filePath))
                {
                    Directory.CreateDirectory(mapPath + filePath);
                }
                webclient.DownloadFile(url, string.Format("{0}{1}{2}", mapPath, filePath, imgFileName));
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 根据图片路径获取图片大小并判断是否需要进行缩略图处理(LCZ添加)
        /// </summary>
        /// <param name="picPath">原始图片路径</param>
        /// <param name="picWidth">欲设置的图片宽度</param>
        /// <param name="picHeight">欲设置的图片高度</param>
        /// <returns>图片是否需要处理</returns>
        public bool EqualWH(string picPath, int picWidth, int picHeight)
        {
            if (File.Exists(mapPath + picPath))
            {
                Bitmap bitmap = new Bitmap(mapPath + picPath);
                if ((bitmap.Width < picWidth) & (bitmap.Height < picHeight))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
